package bma.common.langutil.concurrent;

import java.util.Random;

import junit.framework.TestCase;
import bma.common.langutil.concurrent.RapidUpdateTimeline.Processor;
import bma.common.langutil.core.ObjectUtil;

public class RapidUpdateTimelineTC extends TestCase {

	final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(RapidUpdateTimelineTC.class);

	public void testTimeUp() throws Exception {
		Random random = new Random();
		Processor<Integer> p = new Processor<Integer>() {
			@Override
			public void process(Integer item) {
				System.out.println(item + " time up");
			}
		};
		
		RapidUpdateTimeline<Integer> tm = new RapidUpdateTimeline<Integer>();		
		long now = System.currentTimeMillis();
		for (int i = 1; i <= 100; i++) {
			tm.add(now + 33 * i, i);
		}		
		int idx = 0;
		long nexttm = tm.processTickItems(System.currentTimeMillis(), p);
		while (System.currentTimeMillis() - now < 5 * 1000) {					
			for(int i=1;i<10;i++) {
				tm.remove(random.nextInt(10)+idx*3);
			}
			ObjectUtil.waitFor(this, nexttm-System.currentTimeMillis());			
			System.out.println("round "+idx++);
			nexttm = tm.processTickItems(System.currentTimeMillis(), p);			
		}
		System.out.println(tm.isEmpty()+" - "+"end");
	}

}
